﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using UcAsp.Net.Comm;
namespace UcAsp.Net.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public  string UserAgent
        {
            get
            {
                
                return Request.UserAgent;
            }
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            List<Model.Categories> list = new BLL.CategoriesBLL().GetCategories();
            List<Model.UserDefined> special = BLL.UserDefinedConfig.GetListUserDefinedConfig;
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            foreach (Model.UserDefined model in special)
            {
                routes.MapRoute(
                    model.DefinedName + "UserDefined", // 路由名称
                    "{userdefined}.shtml", // 带有参数的 URL
                    new { controller = "UserDefined", action = "Index", userdefined = UrlParameter.Optional } // 参数默认值
                );
            }
            #region view
            foreach (Model.Categories model in list)
            {
                routes.MapRoute(
                    model.ShowPath + "ShowMessage", // 路由名称
                    model.ShowPath + "/{id}.shtml", // 带有参数的 URL
                    new { controller = "ShowMessage", action = "Index", id = UrlParameter.Optional }, // 参数默认值
                    new { id = @"[\d]{0,50}" }
                );
                routes.MapRoute(
                   model.ShowPath + "MessageShowPath", // 路由名称
                   model.ShowPath + "/{showpath}.shtml", // 带有参数的 URL
                   new { controller = "ShowMessage", action = "ShowPath", showpath = UrlParameter.Optional } // 参数默认值
                   , new { showpath = @"^(?!index)[a-zA-Z0-9]*$" }
               );

                routes.MapRoute(
                   model.ShowPath + "ShowMessagePage", // 路由名称
                   model.ShowPath + "/{id}_{pageindex}.shtml", // 带有参数的 URL
                   new { controller = "ShowMessage", action = "PageShow", id = UrlParameter.Optional, pageindex = UrlParameter.Optional } // 参数默认值
                   , new { id = @"[\d+]{0,50}", pageindex = @"[\d+]{0,50}" }

               );
                routes.MapRoute(
                   model.ShowPath + "MessageShowPagepath", // 路由名称
                    model.ShowPath + "/{showpath}_{pageindex}.shtml", // 带有参数的 URL
                    new { controller = "ShowMessage", action = "PageShow", showpath = UrlParameter.Optional, pageindex = UrlParameter.Optional } // 参数默认值
                    , new { pageindex = @"[\d+]{0,50}", showpath = @"^(?!index)[a-zA-Z0-9]*$" }
                );
            }
            #endregion

            #region 分类

            foreach (Model.Categories model in list)
            {
                routes.MapRoute(
                   model.ShowPath + "Categories", // 路由名称
                    "{showpath}/index.shtml", // 带有参数的 URL
                   new { controller = "Categories", action = "Index", showpath = UrlParameter.Optional } // 参数默认值
               );
                routes.MapRoute(
                  model.ShowPath + "Page", // 路由名称
                   "{showpath}/index_{pageindex}.shtml", // 带有参数的 URL
                  new { controller = "Categories", action = "IndexPage", showpath = UrlParameter.Optional, pageindex = UrlParameter.Optional } // 参数默认值
                  , new { pageindex = @"[\d+]{0,50}" }
              );
            }
            #endregion
            #region 开放接口
            routes.MapRoute(
               "OpenLogin", // 路由名称
               "OpenLogin/Login/{param}.shtml", // 带有参数的 URL
               new { controller = "OpenLogin", action = "Login", param = UrlParameter.Optional } // 参数默认值
           );
            routes.MapRoute(
               "Open", // 路由名称
               "Open/{param}.shtml", // 带有参数的 URL
               new { controller = "OpenLogin", action = "Index", param = UrlParameter.Optional } // 参数默认值
           );
            #endregion

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }
        public static void RegisterViewEngine()
        {
            //ViewEngines.Engines.Clear();            
            ViewEngines.Engines.Insert(0, new UcAsp.Net.Comm.ViewEngine(null, UcAsp.Net.BLL.SettingConfig.GetSiteSetting.Theme));
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            RegisterViewEngine();
        }
        //protected void Application_Error()
        //{

        //    Exception ex = Server.GetLastError().GetBaseException();
        //    Encrypt.ErrorLog("UcAsp.Net", ex.Message + ex.StackTrace, true);
        //    Server.ClearError();
        //    this.Response.Clear();
        //    if (!this.Response.IsRequestBeingRedirected)//在跳转之前做判断,防止重复
        //    {
        //        Response.Redirect("/Error/NoFound", true);
        //    }

        //}
    }
}